#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Export Images & CTF data                                           #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 10/03/2016                                             #
# Last Modification: 26/08/2019                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 30
#
# DISPLAY: export_anything_doit
# DISPLAY: export_basedir
# DISPLAY: export_MultiShot
# DISPLAY: export_rawstack_doit
# DISPLAY: export_rawstack_subdir
# DISPLAY: export_gainref_doit
# DISPLAY: export_gainref_subdir
# DISPLAY: export_pixeldef_doit
# DISPLAY: export_gaincorstack_doit
# DISPLAY: export_gaincorstack_subdir
# DISPLAY: export_driftcorstack_doit
# DISPLAY: export_driftcorstack_subdir
# DISPLAY: export_driftcoraver_doit
# DISPLAY: export_driftcoraver_noDW_doit
# DISPLAY: export_driftcoraver_subdir
# DISPLAY: export_driftcoraver_polish_metadata
# DISPLAY: export_driftcorfig_doit
# DISPLAY: export_driftcorfig_subdir
# DISPLAY: export_CTFDiag_doit
# DISPLAY: export_CTFDiag_subdir
# DISPLAY: export_CTFstar_doit
# DISPLAY: export_CTFstar_subdir
# DISPLAY: export_denoised_subdir
# DISPLAY: export_denoised_doit
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set comment = ""
set export_anything_doit = ""
set export_basedir = ""
set export_MultiShot = ""
set export_rawstack_doit = ""
set export_rawstack_subdir = ""
set export_gainref_doit = ""
set export_gainref_subdir = ""
set export_pixeldef_doit = ""
set export_gaincorstack_doit = ""
set export_gaincorstack_subdir = ""
set export_driftcorstack_doit = ""
set export_driftcorstack_subdir = ""
set export_driftcoraver_doit = ""
set export_driftcoraver_noDW_doit = ""
set export_driftcoraver_polish_metadata = ""
set export_driftcoraver_subdir = ""
set export_denoised_subdir = ""
set export_denoised_doit = ""
set export_driftcorfig_doit = ""
set export_driftcorfig_subdir = ""
set export_CTFDiag_doit = ""
set export_CTFDiag_subdir = ""
set export_CTFstar_doit = ""
set import_rawstack = ""
set import_gainref = ""
set import_defects = ""
set raw_gaincorrectedstack = ""
set movie_stackname = ""
set phacon = ""
set generate_dose_dw = ""
#
#$end_vars
#
set scriptname = export_drift
#
\rm -f LOGS/${scriptname}.results
source ${proc_2dx}/initialize
#
# This memorizes the current merge directory under the variable "olddir":
set olddir = $PWD
#
if ( ${export_anything_doit} != "0" ) then
  #
  cd ${olddir}/LOGS
  if ( ! -d ${export_basedir} ) then
    ${proc_2dx}/linblock "WARNING: ${export_basedir} not existing. Creating it."
    \mkdir -p ${export_basedir}
  endif
  cd ${export_basedir}
  set star_dir = $PWD
  echo ${star_dir}
  cd ${olddir}
  echo "#IMAGE-IMPORTANT: LOGS/${export_basedir}/micrographs_all_defocus.star <STAR file (TXT)>" >> LOGS/${scriptname}.results
  echo "#IMAGE-IMPORTANT: LOGS/${export_basedir}/micrographs_all_defocus_withIciness.star <STAR file with iciness (TXT)>" >> LOGS/${scriptname}.results
  if ( ${export_MultiShot} == "y" ) then
    echo "#IMAGE-IMPORTANT: LOGS/${export_basedir}/micrographs_MultiShot_defocus.star <STAR file (TXT)>" >> LOGS/${scriptname}.results
    echo "#IMAGE-IMPORTANT: LOGS/${export_basedir}/micrographs_MultiShot_defocus_withIciness.star <STAR file with iciness (TXT)>" >> LOGS/${scriptname}.results
  endif
  #
  # This translates the list of directories to work on into one single long line:
  cat 2dx_merge_dirfile.dat | tr "\n" " " > SCRATCH/2dx_merge_dirfile_oneline.dat
  set dirlist = "`cat SCRATCH/2dx_merge_dirfile_oneline.dat`"
  #
  echo "<<@progress: 10>>"
  #
  set dirnum = `cat 2dx_merge_dirfile.dat | wc -l`
  echo ": Will work on ${dirnum} image directories."
  set irun = 1
  #
  # This initiates a big "foreach" loop that sets the variable "dirfile" to each of the items in the "dirlist" and runs the contents of the loop with this value:
  foreach dirfile ( ${dirlist} )
    #
    cd ${olddir}/../${dirfile}
    #
    ${proc_2dx}/linblock "Working on ${dirfile}"
    #
    set import_rawstack = `grep "set import_rawstack =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set import_gainref = `grep "set import_gainref =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set import_defects = `grep "set import_defects =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set raw_gaincorrectedstack = `grep "set raw_gaincorrectedstack =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set movie_stackname = `grep "set movie_stackname =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set KV = `grep "set KV =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set CS = `grep "set CS =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set movie_imagenumber_total = `grep "set movie_imagenumber_total =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set stepdigitizer = `grep "set stepdigitizer =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set iciness = `grep "set iciness =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set AmpCon = ` echo "scale=3; sqrt( 1 - ${phacon} * ${phacon} )" | bc `
    set pixelsize = `grep "set sample_pixel =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set magnification = `echo "scale=3; ${stepdigitizer} * 10000.0 / ${pixelsize}" | bc`
    set defocus = `grep "set defocus =" 2dx_image.cfg | head -1 | cut -d'"' -f2 | sed 's/,/ /g'`
    set defocus_CCvalue = `grep "set defocus_CCvalue =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set defocus_RESMAX = `grep "set defocus_RESMAX =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set defocus_phase_shift = `grep "set defocus_phase_shift =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    if ( ${export_MultiShot} == "y" ) then
      set multishot_series_number = `grep "set multishot_series_number =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    endif
    set frame_dose = `grep "set frame_dose =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set total_dose = `grep "set total_dose =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set import_bin = `grep "set import_bin =" ../../2dx_master.cfg | head -1 | cut -d'"' -f2`

    if ( ${import_bin} == 0 ) then
      set bin_factor = 1
    else
      set bin_factor = 2
    endif
    #
    set local_doit = ${export_anything_doit}
    echo ${export_basedir}
    if ( ${local_doit} == "2" ) then
      set local_doit = 1
    endif
    #
    set oriname = `echo ${import_rawstack} | sed 's/\./\_/g' | sed 's/_mrc//g' | sed 's/_dm4//g' | sed 's/_tif//g' `
    #
    # set sub_export_anything_doit = "${1}"
    # set sub_doit = "${2}"
    # set sub_basedir = "${3}"
    # set sub_targetdir = "${4}"
    # set sub_filename = "${5}"
    # set sub_targetname = "${6}"
    #
    # \rsync -auvP ${sub_filename} ${sub_basedir}/${sub_targetdir}/${sub_targetname}
    #
    source ${proc_2dx}/import_export_sub.com ${export_anything_doit} ${export_rawstack_doit} ${export_basedir} ${export_rawstack_subdir} ${import_rawstack} ${import_rawstack}

    set defects_name = `echo ${import_defects} | rev | cut -d\/ -f1 | rev`
    set gainref_name  = `echo ${import_gainref}  | rev | cut -d\/ -f1 | rev`
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_pixeldef_doit} ${export_basedir} ${export_gainref_subdir} ${import_defects} ${defects_name}
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_gainref_doit}  ${export_basedir} ${export_gainref_subdir} ${import_gainref} ${gainref_name}

    source ${proc_2dx}/import_export_sub.com ${export_anything_doit} ${export_gaincorstack_doit} ${export_basedir} ${export_gaincorstack_subdir} ${raw_gaincorrectedstack}.mrcs ${oriname}_${raw_gaincorrectedstack}.mrcs

    source ${proc_2dx}/import_export_sub.com ${export_anything_doit} ${export_driftcorstack_doit} ${export_basedir} ${export_driftcorstack_subdir} ${movie_stackname}.mrcs ${oriname}_aligned.mrcs

    source ${proc_2dx}/import_export_sub.com ${export_anything_doit} ${export_driftcoraver_doit} ${export_basedir} ${export_driftcoraver_subdir} ${movie_stackname}.mrc ${oriname}.mrc
    source ${proc_2dx}/import_export_sub.com ${export_anything_doit} ${export_denoised_doit} ${export_basedir} ${export_denoised_subdir} ${movie_stackname}_wf-denoised.mrc ${oriname}_wf-denoised.mrc
	source ${proc_2dx}/import_export_sub.com ${export_anything_doit} ${export_driftcoraver_noDW_doit} ${export_basedir} ${export_driftcoraver_subdir} ${movie_stackname}_Sum.mrc ${oriname}_noDW.mrc
    source ${proc_2dx}/export_polish_metadata.com ${export_anything_doit} ${export_driftcoraver_polish_metadata} ${export_basedir} ${export_driftcoraver_subdir} ${movie_stackname}.mrc ${oriname}.mrc

    if ( ${generate_dose_dw}x == "yx" ) then
       source ${proc_2dx}/import_export_sub.com ${export_anything_doit} ${export_driftcoraver_doit} ${export_basedir} ${export_driftcoraver_subdir} ${movie_stackname}_DW-rec.mrc ${oriname}_DW-rec.mrc
    endif
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_driftcorfig_doit} ${export_basedir} ${export_driftcorfig_subdir} translations.png ${oriname}_translations.png
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_driftcorfig_doit} ${export_basedir} ${export_driftcorfig_subdir} 2dx_image.cfg ${oriname}_2dx_image.cfg
    if ( -e motioncor2_shifts.txt ) then
      source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_driftcorfig_doit} ${export_basedir} ${export_driftcorfig_subdir} motioncor2_shifts.txt ${oriname}_motioncor2_shifts.txt
    endif
    #
    source ${proc_2dx}/import_export_sub.com ${export_anything_doit} ${export_CTFDiag_doit} ${export_basedir} ${export_CTFDiag_subdir} CTFDiag.mrc ${oriname}.ctf
    #
    if ( ${export_CTFstar_doit} == "y" ) then
      if ( ! -e ${export_basedir}/micrographs_focus.star ) then
        cat > ${export_basedir}/micrographs_focus.star << eot

data_

loop_
_rlnMicrographName #1
_rlnCtfImage #2
_rlnDefocusU #3
_rlnDefocusV #4
_rlnDefocusAngle #5
_rlnVoltage #6
_rlnSphericalAberration #7
_rlnAmplitudeContrast #8
_rlnMagnification #9
_rlnDetectorPixelSize #10
_rlnCtfFigureOfMerit #11
_rlnCtfMaxResolution #12
_rlnPhaseShift #13
_rlnMicrographMetadata #14
eot
        #
    if ( ${export_MultiShot} == "y" ) echo "_rlnBeamTiltClass #15" >> ${export_basedir}/micrographs_focus.star

    endif

      if ( ${export_driftcoraver_polish_metadata} == "y" ) then

      	set metadata = ${export_driftcoraver_subdir}/${oriname}.star

      else

      	set metadata = "."

      endif

      # if ( ${export_driftcoraver_noDW_doit} == "y" ) then

      #   set noDW_name = ${export_driftcoraver_subdir}/${oriname}_noDW.mrc

      # else

      #   set noDW_name = "."

      # endif


      # First we check if the line already exists in the .star file:

      set starline = "${export_driftcoraver_subdir}/${oriname}.mrc ${export_CTFDiag_subdir}/${oriname}.ctf:mrc ${defocus} ${KV} ${CS} ${AmpCon} ${magnification} ${stepdigitizer} ${defocus_CCvalue} ${defocus_RESMAX} ${defocus_phase_shift} ${metadata}"
      
      if ( ${export_MultiShot} == "y" ) set starline = "${starline} ${multishot_series_number}"

      echo "${starline}" >> ${export_basedir}/micrographs_focus.star

  	 # else
    #   set OnlyUseCTFFIND = 0 #temporary LK 05.07.2017. Definition missing.
    #   if ( ${OnlyUseCTFFIND} == 1 ) then
    #     ${proc_2dx}/protest "ERROR: CTFFIND4 results present, but not for all images. Aborting."
    #   endif
      # Use output of gCTF
      # if ( ! -e ${export_basedir}/micrographs_focus.star ) then  #micrographs_all_defocus.star
      #   head -n 16 micrographs_all_.ctf:mrc.star > ${export_basedir}/micrographs_all_defocus.star
      #   head -n 16 micrographs_all_.ctf:mrc.star > ${export_basedir}/micrographs_all_defocus_withIciness.star
      #   echo "_rlnIciness #13" >> ${export_basedir}/micrographs_all_.ctf:mrc_withIciness.star
      # endif
      # echo "${export_driftcoraver_subdir}/${oriname}.mrc ${export_CTFDiag_subdir}/${oriname}.ctf:mrc" `tail -n 2 micrographs_all_.ctf:mrc.star | head -n 1 | cut -d\  -f3-`            >> ${export_basedir}/micrographs_focus.star
      # echo "${export_driftcoraver_subdir}/${oriname}.mrc ${export_CTFDiag_subdir}/${oriname}.ctf:mrc" `tail -n 2 micrographs_all_.ctf:mrc.star | head -n 1 | cut -d\  -f3-` ${iciness} >> ${export_basedir}/micrographs_all_defocus_withIciness.star

    endif
    if ( ${export_CTFDiag_doit} == "y" ) then
	    if ( -e diagnostic_output.txt ) then
	      # Use output of CTFFIND4
	      \rsync -auvP diagnostic_output.txt ${export_basedir}/${export_CTFDiag_subdir}/${oriname}_diagnostic_output.txt
	      \rsync -auvP diagnostic_output_avrot.txt ${export_basedir}/${export_CTFDiag_subdir}/${oriname}_diagnostic_output_avrot.txt
	    endif
	endif
    if ( -e ${movie_stackname}_Sum_.ctf:mrc.log ) then
      # Use output of gCTF
      cat ${movie_stackname}_Sum_.ctf:mrc.log | sed s/${movie_stackname}_Sum/${oriname}/g > ${oriname}.ctf:mrcfind3.log
      source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_CTFstar_doit} ${export_basedir} ${export_CTFDiag_subdir} ${oriname}.ctf:mrcfind3.log ${oriname}.ctf:mrcfind3.log
    endif
    #
    echo ":   Line added to Star file ${export_basedir}/micrographs_focus.star"
    echo ": "
    #
    echo ": "
    echo ":Exported to:"
    echo ":${export_basedir}"
    echo ": "
    #
    @ irun += 1
    set progress = `echo ${irun} ${dirnum} | awk '{ s = 10 + int( 80 * $1 / $2 ) } END { print s }'`
    echo "<<@progress:${progress}>>"
    #
    cd ${olddir}
  end
else
  #
  echo ":: Not Running. "
  #
endif
#
echo "<<@progress: 100>>"
echo "<<@evaluate>>"
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
##
#
#
#
